package org.dromara.school.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.school.domain.vo.ClassesVo;
import org.dromara.school.domain.bo.ClassesBo;
import org.dromara.school.service.IClassesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 班级管理
 *
 * @author Gjb
 * @date 2025-05-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/school/classes")
public class ClassesController extends BaseController {

    private final IClassesService classesService;

    /**
     * 查询班级管理列表
     */
    @SaCheckPermission("school:classes:list")
    @GetMapping("/list")
    public TableDataInfo<ClassesVo> list(ClassesBo bo, PageQuery pageQuery) {
        return classesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出班级管理列表
     */
    @SaCheckPermission("school:classes:export")
    @Log(title = "班级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ClassesBo bo, HttpServletResponse response) {
        List<ClassesVo> list = classesService.queryList(bo);
        ExcelUtil.exportExcel(list, "班级管理", ClassesVo.class, response);
    }

    /**
     * 获取班级管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("school:classes:query")
    @GetMapping("/{id}")
    public R<ClassesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(classesService.queryById(id));
    }

    /**
     * 新增班级管理
     */
    @SaCheckPermission("school:classes:add")
    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ClassesBo bo) {
        return toAjax(classesService.insertByBo(bo));
    }

    /**
     * 修改班级管理
     */
    @SaCheckPermission("school:classes:edit")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ClassesBo bo) {
        return toAjax(classesService.updateByBo(bo));
    }

    /**
     * 删除班级管理
     *
     *
     * @param ids 主键串
     */
    @SaCheckPermission("school:classes:remove")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(classesService.deleteWithValidByIds(List.of(ids), true));
    }


    @SaCheckPermission("school:classes:query")
    @GetMapping("/getgrades/{schoolCampusId}")
    public R getSchoolGrades(@PathVariable Long schoolCampusId){
        return R.ok(classesService.getSchoolGrades(schoolCampusId));
    }

    /**
     * 根据校区获取班级
     * @param schoolCampusId
     * @return
     */
    @GetMapping(value = "/getclasses/{schoolCampusId}")
    public R getSchoolClasses(@PathVariable Long schoolCampusId){
        return R.ok( classesService.getSchoolClasses(schoolCampusId));
    }

    /**
     * 获取所有班级节点
     * @return
     */
    @GetMapping(value = "/getSchoolClassesTreeNode")
    public R getSchoolClassesTreeNode(){

        return R.ok(classesService.getSchoolClassesTreeNode());
    }






}
